package tttrung43.customview;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.lopngoaingu.R;

public class DynamicViewActivity extends Activity {

	Button btnAdd, btnClear;
	LinearLayout layoutParent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dynamic_layout);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnClear = (Button) findViewById(R.id.btnClear);
		layoutParent = (LinearLayout) findViewById(R.id.layoutParent);
		btnAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AddButton();
				
			}
		});

		btnClear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				layoutParent.removeAllViews();
			}
		});
	}

	private void CreateRandom(String word) {
		int len = word.length();		
		
		Random ran = new Random();
		for (int i = 0; i < len; i++) {
			Button btn = new Button(this);
			btn.setText(""+word.getBytes()[ran.nextInt(len-1)]);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParent.addView(btn, params);
		}
	}

	private void AddButton() {
		Button btn = new Button(this);
		btn.setText("A");
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParent.addView(btn, params);
	}

}
